package com.ruoyi.project.instrument.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.instrument.domain.InsInstrument;
import com.ruoyi.project.instrument.service.IInsInstrumentService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 仪器信息Controller
 *
 * @author taney
 * @date 2020-06-08
 */
@AllArgsConstructor
@RestController
@RequestMapping("/instrument/instrument")
public class InsInstrumentController extends BaseController {

    private final IInsInstrumentService iInsInstrumentService;

    /**
     * 查询仪器信息列表
     */
    @PreAuthorize("@ss.hasPermi('instrument:instrument:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsInstrument insInstrument) {
        startPage();
        LambdaQueryWrapper<InsInstrument> lqw = new LambdaQueryWrapper<InsInstrument>();
        if (StringUtils.isNotBlank(insInstrument.getState())) {
            lqw.eq(InsInstrument::getState, insInstrument.getState());
        }
        if (StringUtils.isNotBlank(insInstrument.getNetState())) {
            lqw.eq(InsInstrument::getNetState, insInstrument.getNetState());
        }
        List<InsInstrument> list = iInsInstrumentService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出仪器信息列表
     */
    @PreAuthorize("@ss.hasPermi('instrument:instrument:export')")
    @Log(title = "仪器信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InsInstrument insInstrument) {
        LambdaQueryWrapper<InsInstrument> lqw = new LambdaQueryWrapper<InsInstrument>(insInstrument);
        List<InsInstrument> list = iInsInstrumentService.list(lqw);
        ExcelUtil<InsInstrument> util = new ExcelUtil<InsInstrument>(InsInstrument.class);
        return util.exportExcel(list, "instrument");
    }

    /**
     * 获取仪器信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('instrument:instrument:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(iInsInstrumentService.getById(id));
    }

    /**
     * 新增仪器信息
     */
    @PreAuthorize("@ss.hasPermi('instrument:instrument:add')")
    @Log(title = "仪器信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InsInstrument insInstrument) {
        return toAjax(iInsInstrumentService.save(insInstrument) ? 1 : 0);
    }

    /**
     * 修改仪器信息
     */
    @PreAuthorize("@ss.hasPermi('instrument:instrument:edit')")
    @Log(title = "仪器信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InsInstrument insInstrument) {
        return toAjax(iInsInstrumentService.updateById(insInstrument) ? 1 : 0);
    }

    /**
     * 删除仪器信息
     */
    @PreAuthorize("@ss.hasPermi('instrument:instrument:remove')")
    @Log(title = "仪器信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(iInsInstrumentService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 仪器定标指令下发
     */
    @PreAuthorize("@ss.hasPermi('instrument:instrument:calibration')")
    @Log(title = "下发仪器定标指令", businessType = BusinessType.UPDATE)
    @GetMapping("calibration/{ids}")
    public AjaxResult calibration(@PathVariable Integer[] ids) {
        return iInsInstrumentService.calibration(Arrays.asList(ids));
    }

    /**
     * 仪器软件更新指令
     */
    @PreAuthorize("@ss.hasPermi('instrument:instrument:versionUpdate')")
    @Log(title = "下发仪器软件版本更新指令", businessType = BusinessType.UPDATE)
    @GetMapping("versionUpdate/{ids}")
    public AjaxResult versionUpdate(@PathVariable Integer[] ids) {
        return iInsInstrumentService.versionUpdate(Arrays.asList(ids));
    }

    /**
     * 仪器关机指令下发
     */
    @PreAuthorize("@ss.hasPermi('instrument:instrument:powerOff')")
    @Log(title = "下发仪器关机指令", businessType = BusinessType.UPDATE)
    @PostMapping("powerOff/{ids}")
    public AjaxResult powerOff(@PathVariable Integer[] ids,String state,String netState) {
        return iInsInstrumentService.powerOff(Arrays.asList(ids),state,netState);
    }
}
